﻿namespace Hidistro.UI.Web.Handler
{
    using Hidistro.Core;
    using Hidistro.Entities;
    using Hidistro.Membership.Context;
    using Hidistro.UI.Common.Controls;
    using Hidistro.UI.ControlPanel.utility;
    using Hidistro.UI.SaleSystem.Tags;
    using Hishop.Components.Validation;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    public class DesigAdvert : AdminPage, IHttpHandler
    {
        private string elementId = "";
        private string message = "";
        private string modeId = "";
        private string resultformat = "{{\"success\":{0},\"Result\":{1}}}";

        private bool CheckAdvertCustom(JObject advertcustomobject)
        {
            if (!string.IsNullOrEmpty(advertcustomobject["Id"].ToString()) && (advertcustomobject["Id"].ToString().Split(new char[] { '_' }).Length == 2))
            {
                return true;
            }
            this.message = string.Format(this.resultformat, "false", "\"请选择要编辑的对象\"");
            return false;
        }

        private bool CheckAdvertImage(JObject advertimageobject)
        {
            if (string.IsNullOrEmpty(advertimageobject["Image"].ToString()))
            {
                this.message = string.Format(this.resultformat, "false", "\"请选择广告图片！\"");
                return false;
            }
            if (!string.IsNullOrEmpty(advertimageobject["Id"].ToString()) && (advertimageobject["Id"].ToString().Split(new char[] { '_' }).Length == 2))
            {
                return true;
            }
            this.message = string.Format(this.resultformat, "false", "\"请选择要编辑的对象\"");
            return false;
        }

        private bool CheckAdvertSlide(JObject avdvertobject)
        {
            if ((((string.IsNullOrEmpty(avdvertobject["Image1"].ToString()) && string.IsNullOrEmpty(avdvertobject["Image2"].ToString())) && (string.IsNullOrEmpty(avdvertobject["Image3"].ToString()) && string.IsNullOrEmpty(avdvertobject["Image4"].ToString()))) && ((string.IsNullOrEmpty(avdvertobject["Image5"].ToString()) && string.IsNullOrEmpty(avdvertobject["Image6"].ToString())) && (string.IsNullOrEmpty(avdvertobject["Image7"].ToString()) && string.IsNullOrEmpty(avdvertobject["Image8"].ToString())))) && (string.IsNullOrEmpty(avdvertobject["Image9"].ToString()) && string.IsNullOrEmpty(avdvertobject["Image10"].ToString())))
            {
                this.message = string.Format(this.resultformat, "false", "\"请至少上传3张广告图片！\"");
                return false;
            }
            int num = 0;
            for (int i = 1; i <= 10; i++)
            {
                if (!string.IsNullOrEmpty(avdvertobject["Image" + i].ToString()))
                {
                    num++;
                }
            }
            if (num < 3)
            {
                this.message = string.Format(this.resultformat, "false", "\"请至少上传3张广告图片！\"");
                return false;
            }
            if (!string.IsNullOrEmpty(avdvertobject["Id"].ToString()) && (avdvertobject["Id"].ToString().Split(new char[] { '_' }).Length == 2))
            {
                return true;
            }
            this.message = string.Format(this.resultformat, "false", "\"请选择要编辑的对象\"");
            return false;
        }

        private bool CheckLogo(JObject logoobject)
        {
            if (string.IsNullOrEmpty(logoobject["LogoUrl"].ToString()))
            {
                this.message = string.Format(this.resultformat, "false", "\"请上传Logo图片！\"");
                return false;
            }
            return true;
        }

        public Dictionary<string, string> GetXmlNodeString(JObject scriptobject)
        {
            Dictionary<string, string> dictionary = scriptobject.ToObject<Dictionary<string, string>>();
            Dictionary<string, string> dictionary2 = new Dictionary<string, string>();
            foreach (KeyValuePair<string, string> pair in dictionary)
            {
                dictionary2.Add(pair.Key, Globals.HtmlEncode(pair.Value.ToString()));
            }
            return dictionary2;
        }

        public new void ProcessRequest(HttpContext context)
        {
            try
            {
                this.modeId = context.Request.Form["ModelId"];
                string a;
                if ((a = this.modeId) != null)
                {
                    if (!(a == "editeadvertslide"))
                    {
                        if (!(a == "editeadvertimage"))
                        {
                            if (!(a == "editelogo"))
                            {
                                if (a == "editeadvertcustom")
                                {
                                    string text = context.Request.Form["Param"];
                                    if (text != "")
                                    {
                                        JObject advertcustomobject = (JObject)JsonConvert.DeserializeObject(text);
                                        if (this.CheckAdvertCustom(advertcustomobject) && this.UpdateAdvertCustom(advertcustomobject))
                                        {
                                            var value = new
                                            {
                                                AdCustom = new Common_CustomAd
                                                {
                                                    AdId = Convert.ToInt32(this.elementId)
                                                }.RendHtml()
                                            };
                                            this.message = string.Format(this.resultformat, "true", JsonConvert.SerializeObject(value));
                                        }
                                    }
                                }
                            }
                            else
                            {
                                string text2 = context.Request.Form["Param"];
                                if (text2 != "")
                                {
                                    JObject jObject = (JObject)JsonConvert.DeserializeObject(text2);
                                    if (this.CheckLogo(jObject) && this.UpdateLogo(jObject))
                                    {
                                        Common_Logo common_Logo = new Common_Logo();
                                        var value2 = new
                                        {
                                            LogoUrl = common_Logo.RendHtml()
                                        };
                                        this.message = string.Format(this.resultformat, "true", JsonConvert.SerializeObject(value2));
                                    }
                                }
                            }
                        }
                        else
                        {
                            string text3 = context.Request.Form["Param"];
                            if (text3 != "")
                            {
                                JObject advertimageobject = (JObject)JsonConvert.DeserializeObject(text3);
                                if (this.CheckAdvertImage(advertimageobject) && this.UpdateAdvertImage(advertimageobject))
                                {
                                    var value3 = new
                                    {
                                        AdImage = new Common_ImageAd
                                        {
                                            AdId = Convert.ToInt32(this.elementId)
                                        }.RendHtml()
                                    };
                                    this.message = string.Format(this.resultformat, "true", JsonConvert.SerializeObject(value3));
                                }
                            }
                        }
                    }
                    else
                    {
                        string text4 = context.Request.Form["Param"];
                        if (text4 != "")
                        {
                            JObject avdvertobject = (JObject)JsonConvert.DeserializeObject(text4);
                            if (this.CheckAdvertSlide(avdvertobject) && this.UpdateAdvertSlide(avdvertobject))
                            {
                                var value4 = new
                                {
                                    AdSlide = new Common_SlideAd
                                    {
                                        AdId = Convert.ToInt32(this.elementId)
                                    }.RendHtml()
                                };
                                this.message = string.Format(this.resultformat, "true", JsonConvert.SerializeObject(value4));
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                this.message = "{\"success\":false,\"Result\":\"未知错误:" + ex.Message + "\"}";
            }
            context.Response.ContentType = "text/json";
            context.Response.Write(this.message);
        }

        private bool UpdateAdvertCustom(JObject advertcustomobject)
        {
            this.message = string.Format(this.resultformat, "false", "\"自定义编辑失败\"");
            this.elementId = advertcustomobject["Id"].ToString().Split(new char[] { '_' })[1];
            advertcustomobject["Id"] = this.elementId;
            Dictionary<string, string> xmlNodeString = this.GetXmlNodeString(advertcustomobject);
            if (xmlNodeString.Keys.Contains<string>("Html"))
            {
                xmlNodeString["Html"] = Globals.HtmlDecode(xmlNodeString["Html"]);
            }
            return TagsHelper.UpdateAdNode(Convert.ToInt16(this.elementId), "custom", xmlNodeString);
        }

        private bool UpdateAdvertImage(JObject advertimageobject)
        {
            this.message = string.Format(this.resultformat, "false", "\"修改单张广告图片失败\"");
            this.elementId = advertimageobject["Id"].ToString().Split(new char[] { '_' })[1];
            advertimageobject["Id"] = this.elementId;
            Dictionary<string, string> xmlNodeString = this.GetXmlNodeString(advertimageobject);
            return TagsHelper.UpdateAdNode(Convert.ToInt16(this.elementId), "image", xmlNodeString);
        }

        private bool UpdateAdvertSlide(JObject avdvertobject)
        {
            this.message = string.Format(this.resultformat, "false", "\"修改轮播广告失败\"");
            this.elementId = avdvertobject["Id"].ToString().Split(new char[] { '_' })[1];
            avdvertobject["Id"] = this.elementId;
            Dictionary<string, string> xmlNodeString = this.GetXmlNodeString(avdvertobject);
            return TagsHelper.UpdateAdNode(Convert.ToInt16(this.elementId), "slide", xmlNodeString);
        }

        private bool UpdateLogo(JObject advertimageobject)
        {
            this.message = string.Format(this.resultformat, "false", "\"修改Logo图片失败\"");
            SiteSettings siteSettings = HiContext.Current.SiteSettings;
            siteSettings.LogoUrl = advertimageobject["LogoUrl"].ToString();
            SettingsManager.Save(siteSettings);
            return true;
        }

        private bool ValidationSettings(SiteSettings setting, ref string errors)
        {
            ValidationResults results = Hishop.Components.Validation.Validation.Validate<SiteSettings>(setting, new string[] { "ValMasterSettings" });
            if (!results.IsValid)
            {
                foreach (ValidationResult result in (IEnumerable<ValidationResult>) results)
                {
                    errors = errors + Formatter.FormatErrorMessage(result.Message);
                }
            }
            return results.IsValid;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

